package com.stox.indicator;

import java.util.List;

import org.springframework.stereotype.Component;

import com.stox.dto.Bar;
import com.stox.dto.BarValue;

@Component
public class ExponentialMovingAverage extends AbstractMovingAverage {

	public Double[] getValues(Configuration configuration, Double[] values){
		Double[] result = new Double[values.length];
		int span = configuration.getSpan();
		int size = values.length;
		double multiplier = 2/(new Double(span) + 1);
		double previousEMA = 0;
		for(int i = size - 1; i >= 0; i--){
			if(null == values[i]) continue;
			previousEMA = values[i]*multiplier + previousEMA*(1 - multiplier);
			if(i <= size - 1 - span){
				result[i] = previousEMA;
			}
		}
		return result;
	}
	
	@Override
	public Double[] getValues(Configuration configuration, List<Bar> bars) {
		Double[] result = new Double[bars.size()];
		int span = configuration.getSpan();
		BarValue barValue = configuration.getBarValue();
		int size = bars.size();
		double multiplier = 2/(new Double(span) + 1);
		double previousEMA = 0;
		for(int i = size - 1; i >= 0; i--){
			previousEMA = barValue.get(bars.get(i))*multiplier + previousEMA*(1 - multiplier);
			if(i <= size - 1 - span){
				result[i] = previousEMA;
			}
		}
		return result;
	}

	@Override
	public Double getValue(Configuration configuration, List<Bar> bars,
			int index) {
		throw new UnsupportedOperationException("Yet!!!");
	}
	
}
